﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using HelloMVC.Model;
using HelloMVC.Injection;
using HelloMVC.Service;

namespace HelloMVC
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        #region Member Vars
        private static DependencyInjectHandler _diHandler;
        #endregion

        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}.mvc/{action}/{id}", // URL with parameters
                new { action = "Index", id = "" } // Parameter defaults
            );

            routes.MapRoute(
                "Root",
                "",
                new { controller = "Home", action = "Index", id = "" }
            );
        }

        private static void SetupControllerFactory()
        {
            _diHandler = new DependencyInjectHandler(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "Config\\Castle.xml");
            ControllerBuilder.Current.SetControllerFactory(new PIMControllerFactory(_diHandler));
        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            SetupControllerFactory();

            ModelMetadataProviders.Current = new MyMetadataProvider();

            log4net.Config.XmlConfigurator.Configure();
        }
    }
}